Ext.namespace("GridPartition");

GridPartition.Detail = function(plist) {
	
	this.parentList = plist;
	this.userSelector = null;
	this.selectUserId = null;
	this.users = [];
	
	this.txtAreaId = new Ext.form.NumberField({
		fieldLabel: '区域编号',
		disabled: true,
		width: 80
	});
	
	this.hdGisId = new Ext.form.Hidden();
	
	this.txtName = new Ext.form.TextField({
		fieldLabel: '区域名称',
		width: 200,
		allowBlank: false,
        blankText: '请输入区域名称!',
		maxLength : 25
	});
	
    this.cmbType = new Ext.form.ComboBox({
        width: 100,
        fieldLabel: '区域类型',
        readOnly: true,
        store: OMS.GRID_TYPE,
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
    });
    
    this.allRegionStore = new Ext.data.JsonStore({
		   url:"../gridareas/listAllRegions.form",
		   autoLoad : true,
		   root:"regions",
		   fields:["areaId","name"]
	});
	
	this.cmbRegion = new Ext.form.ComboBox({
		fieldLabel :'所在区域',
		width :100,
		store : this.allRegionStore,
		valueField : 'areaId',
		displayField :'name',
        readOnly: true,
		typeAhead :true,
        mode: 'local',
        editable: false,
		triggerAction :'all',
		emptyText :'请选择所在区域',
		hidden : true ,
		hideMode: 'offsets',
		//hideLabel : true,
		selectOnFocus :true	
	});
	
    this.txtDescription = new Ext.form.TextArea({
    	fieldLabel: '区域简介',
		width: 200,
		maxLength : 2000
	});
 
	this.txtGridManager = new Ext.form.TextField({
		fieldLabel: '区域经理',
		readOnly: true,
		width: 85,
		listeners: {
		   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt;
				//console.info(self)
				cmp.getEl().on('click', self.showSelector, self); 
			}
		}
	});	
        
//    this.txtContact = new Ext.form.TextField({
//    	fieldLabel: '联系方式',
//    	width: 150
//    });
//    
//    this.txtGIS = new Ext.form.TextArea({
//    	fieldLabel: 'GIS信息',
//    	width: 200
//    });
	
	this.form = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 75,
        labelAlign: 'right',
        width: 420,
        items: [this.txtAreaId,this.txtName,this.cmbType,this.cmbRegion,this.txtGridManager,this.txtDescription,this.hdGisId]
    });
	
	GridPartition.Detail.superclass.constructor.call(this, {
		title: '区域修改',
        bodyStyle: 'padding: 5px 5px 0',
        autoHeight: true,
        width: 450,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form],
        buttons: [
        /*{
            text: '地图查看',
            handler: this.onMapView,
            scope: this
        },*/
        {
            text: '保存',
            cls: 'x-btn-text-icon btn-save',
            handler: this.onSave,
            scope: this
        }, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
	});
	
	this.connSave = new Ext.data.Connection({
		url: '../gridareas/save.form?timesnap=' + Date.parse(new Date()),
		method: 'POST'
	});
};
Ext.extend(GridPartition.Detail, Ext.Window, {
	setValues: function(data){
		//console.info(data);
		this.txtAreaId.setValue(data.areaId);
		this.txtName.setValue(data.name);
		var type = data.type;
		this.cmbType.setValue(type);
		this.txtDescription.setValue(data.description);
		this.hdGisId.setValue(data.gisId);
		this.selectUserId = data.gridManager;
		this.txtGridManager.setValue(data.gridManagerName);
		this.users.length=0;
		this.users.push({'id':data.gridManager,'name':data.gridManagerName});
		this.cmbRegion.setValue(data.pid);
		if(type=='1'){
			this.cmbRegion.show();
			this.cmbRegion.getEl().up('.x-form-item').setDisplayed(true);// show label
		}else{
			this.cmbRegion.hide();
			this.cmbRegion.getEl().up('.x-form-item').setDisplayed(false); // hide label
		}
	},
	onSave: function(btn, event) {
		if (!this.form.getForm().isValid()) {
			return;
		}
		Utils.request(this.connSave, {
			params: {
				areaId: this.txtAreaId.getValue(),
				name: this.txtName.getValue(),
				type:this.cmbType.getValue(),
				description: this.txtDescription.getValue() ,
				gridManager:this.selectUserId,				
				gisId:this.hdGisId.getValue(),
				pid:this.cmbRegion.getValue()
			},
			scope: this,
			success: function(resp, action) {
				this.scope.parentList.update();
				this.scope.hide();
			},
			after: function() {}
		});
	},	
	onMapView: function(btn, event) {
		var gisId = this.hdGisId.getValue();
		var type = this.cmbType.getValue();
		if (gisId==null||gisId=='') {
			return;
		}
		var url = OMS.GIS_IP+'/gis_for_management_simple.jsp?did=' + gisId +  '&type=' +type;
		Utils.openWindow(url,'区域地图');
	},	
	showSelector: function(event) {
		if (!this.userSelector) {
			this.userSelector = new Selector.User(this, '区域经理');
		}
		//console.info(this.users);
		this.userSelector.initUsers(this.users);
		this.userSelector.show();
	},
	setUsers: function(o) {	// for Selector.User callback
		if(o.length>0){
			this.users = o;
			this.txtGridManager.setValue(this.users[0].name);
			this.selectUserId = this.users[0].id;
		}
		
	}
});

